package com.hoshiicloud.gateway.util;

import com.alibaba.fastjson.JSONObject;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.rpc.Response;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * @program: hoshiibuy
 * @description: 网关过滤器工具类
 * @author: Mr.wu
 * @create: 2019-07-02 15:40
 **/
public class FilterUtil {
    public static Mono<Void> getUnauthResponse(ServerHttpResponse response){
        Response result= Response.failed(CommonError.unauthorizedError());
        byte[] resultByte= JSONObject.toJSONBytes(result);
        DataBuffer buffer=response.bufferFactory().wrap(resultByte);
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    public static Mono<Void> getFailedResponse(ServerHttpResponse response,Response result){
        byte[] resultByte= JSONObject.toJSONBytes(result);
        DataBuffer buffer=response.bufferFactory().wrap(resultByte);
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    public static Mono<Void> getUnauthResponse2(ServerHttpResponse response){
        Response result= Response.failed(CommonError.LoginOtherError());
        byte[] resultByte= JSONObject.toJSONBytes(result);
        DataBuffer buffer=response.bufferFactory().wrap(resultByte);
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }
}
